<template>
  <var-popup v-model:show="dialog" position="bottom" :style="{ height: '80%', backgroundColor: 'transparent' }">
    <div class="full-width full-height relative-position">
      <div class="agreement-box">
        <div class="agreement-box-text">
          阅读并同意协议
          <img @click="close" style="width: 16px;height: 16px;" src="~/assets/images/public/close.png" alt="">
        </div>
        <div class="style-bg"></div>
        <div class="change-tab">
          <div class="tab-box" :style="{
            background: currentIndex == 0 ? 'linear-gradient(149deg, rgba(255,255,255,1) 0%, rgba(233,238,244,1) 60%)' : 'linear-gradient(149deg, rgba(233,238,244,1) 60%, rgba(255,255,255,1) 100%)'
          }">
            <img v-show="currentIndex == 0" style="height: 56px; width: 100%;" src="~/assets/images/public/popup-tab.png" alt="">
            <img v-show="currentIndex == 1" style="height: 56px; width: 100%;" src="~/assets/images/public/popup-tab-left.png" alt="">
            <div class="tab-box-text">
              <div style="width: 100%; display: flex; align-items: center; justify-content: center;" @click="changeTab(0)" :class="[currentIndex == 0 ? 'tab-box-text-isActive' : '']">{{ leftName }}</div>
              <div style="width: 100%; display: flex; align-items: center; justify-content: center;" @click="changeTab(1)" :class="[currentIndex == 1 ? 'tab-box-text-isActive' : '']">{{ rightName }}</div>
            </div>
          </div>
          <div class="main-box" :style="{ 
              borderRadius: currentIndex == 0 ? '0 10px 0 0' : '10px 0 0 0',
            }">
            <iframe v-if="currentIndex == 0" style="width: 100%; flex: auto;" scrolling="yes" :src="privacy" frameborder="0"></iframe>
            <iframe v-if="currentIndex == 1" style="width: 100%; flex: auto;" scrolling="yes" :src="privacyLevel" frameborder="0"></iframe>
            <div style="width: 319px;">
              <q-btn label="同意协议并继续" padding="none none" unelevated rounded @click="submit" :loading="loading"
                class="full-width" style="
                  background-color: #1277FF;
                  color: #fff;
                  height: 52px;
                  font-size: 15px;
                  font-weight: 600;
                " />
            </div>
          </div>
          <div class="main-bg-box"></div>
        </div>
      </div>
    </div>
  </var-popup>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { Show } from './AgreementDialog';
import { useWxHook } from 'src/hooks/wx_hooks';
import { useRoute, useRouter } from 'vue-router';
import { useProtocolLink } from 'src/config/protocol_link';
const props = defineProps({
  leftName: {
    default: "用户服务协议",
    type: String,
  },
  rightName: {
    default: "隐私政策",
    type: String,
  },
  privacy: {
    default: useProtocolLink().userServicesAgreement,
    type: String,
  },
  privacyLevel: {
    default: useProtocolLink().privacyPolicy,
    type: String,
  },
})
const dialog = ref(false);
const loading = ref(false);
const currentIndex = ref(0)
const router = useRouter();
const route = useRoute();
const emit = defineEmits(['confirm'])
const show: Show = async (index = 0) => {
  currentIndex.value = index
  dialog.value = true;
};
const changeTab = (index: number) => {
  currentIndex.value = index
}


const submit = () => {
  close()
  emit('confirm')
};

const close = () => {
  dialog.value = false;
};
defineExpose({ show });
</script>

<style lang="scss" scoped>
.close-btn {
  position: absolute;
  right: 0;
  top: 0;
  margin: 16px;
}
.agreement-box {
  background-image: url(src/assets/images/public/popup-top.png);
  background-color: #F4F6F9;
  background-repeat: no-repeat;
  background-size: 100% auto;
  border-radius: 20px 20px 0 0;
  overflow: hidden;
  height: 100%;
  padding: 20px 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  .style-bg{
    position: absolute;
    width: 100%;
    height: 182px;
    top: 50px;
    background: linear-gradient( 180deg, rgba(255,255,255,0) 0%, rgba(255,255,255,0.95) 20%, #F4F6F9 100%);
  }
  .change-tab{
    width: 100%;
    position: relative;
    display: flex;
    flex-direction: column;
    margin-top: 17px;
    flex: auto;
    .main-bg-box{
      background: #e9eef4;
      position: absolute;
      width: 100%;
      height: 50px;
      top: 56px;
      z-index: 10;
    }
    .main-box{
      display: flex;
      flex-direction: column;
      align-items: center;
      position: relative;
      background: rgb(255,255,255);
      z-index: 11;
      padding: 6px 13px;
      flex: 1 0 auto;
      width: 100%;    
      overflow-y: scroll;     
      -webkit-overflow-scrolling: touch;
    }
    .tab-box{
      display: flex;
      background: rgb(255,255,255);
      border-radius: 16px 16px 0 0;
      &-text{
        position: absolute;
        display: flex;
        justify-content: space-between;
        width: 100%;
        font-weight: 500;
        font-size: 16px;
        color: #222222;
        line-height: 22px;
        height: 56px;
      }
      .tab-box-text-isActive{
        color: #1277FF;
      }
    }
  }
  &-text {
    font-size: 18px;
    color: #131D3F;
    line-height: 25px;
    font-weight: 600;
    display: flex;
    width: 100%;
    justify-content: space-between;
    align-items: center;
    padding: 0 4px;
  }
}

.submit-btn {
  background-color: #1277ff;
  color: #fff !important;
  font-size: 16px;
  font-weight: bold;
  letter-spacing: 1px;
  color: #000;

  .tips {
    position: absolute;
    $width: 120px;
    width: $width;
    top: -0.225 * $width * 0.9;
    height: 0.225 * $width;
    background: url(src/assets/images/home/btn-tips.png) no-repeat;
    background-size: 100% 100%;
  }

  .pointer {
    position: absolute;
    right: calc(10% - 0px);
    top: calc(50% - 4px);
    z-index: 1;

    img {
      position: abs;
      z-index: 1;
      animation: scale-anm 2s infinite;
    }
  }
}

@keyframes scale-anm {
  0% {
    transform: scale(1);
  }

  25% {
    transform: scale(1.2);
  }

  50% {
    transform: scale(1);
  }

  75% {
    transform: scale(1.2);
  }

  100% {
    transform: scale(1);
  }
}
</style>